package com.hujf.flink.DataSet;

import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.api.java.DataSet;
import org.apache.flink.api.java.ExecutionEnvironment;
import org.apache.flink.api.java.operators.ReduceOperator;
import org.apache.flink.api.java.operators.UnsortedGrouping;
import org.apache.flink.api.java.tuple.Tuple3;

/**
 * @Author hujf
 * @Date 2022/5/19 10:43
 * @Description TODO
 */
public class CountDataSet {
    public static void main(String[] args) throws Exception {
        ExecutionEnvironment environment = ExecutionEnvironment.getExecutionEnvironment();


        DataSet<Tuple3<Integer, String, Double>> tuple2DataSource = environment.fromElements(new Tuple3<>(2,"BMW",1.0),
                new Tuple3<>(2, "BMW",2.0),
                new Tuple3<>(3, "TSL",3.0),
                new Tuple3<>(4,"TSL",4.0));

        ReduceOperator<Tuple3<Integer, String, Double>> reduce = tuple2DataSource.groupBy(1, 0).reduce(new ReduceFunction<Tuple3<Integer, String, Double>>() {
            @Override
            public Tuple3<Integer, String, Double> reduce(Tuple3<Integer, String, Double> value1, Tuple3<Integer, String, Double> value2) throws Exception {
                return new Tuple3<>(value1.f0 , value1.f1, value2.f2+value1.f2);
            }
        });

        reduce.print();
    }
}
